/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.sudoku.negocio;

import java.io.Serializable;

import org.apache.commons.logging.LogFactory;
import org.gonalge.sudoku.dto.Nodo;

/**
 * 
 * @author Geremias Gonzalez
 */
public class Lista implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 2725441069220584109L;

    private Nodo tope;

    private int size = 0;

    public void addicionarEle(Object ele) {
        if (tope == null) {
            tope = new Nodo(ele);
            size = 1;
        } else {
            Nodo nodo = new Nodo(ele);
            nodo.setNodoSig(tope);
            tope = nodo;
            size++;
        }
    }

    public boolean isEmpty() {
        return tope == null;
    }

    /**
     * 
     * Obtiene el elemento del nodo actual y despues avanza
     * 
     * @return
     */
    public Object getEleNext() {
        Object ele = null;
        if (!isEmpty()) {
            Nodo a = tope;
            tope = tope.getNodoSig();
            size--;
            ele = a.getEle();
        }
        return ele;
    }

    public Object getEle(int index) {
        Nodo a = tope;
        while (index != 0) {
            a = a.getNodoSig();
            index--;
        }
        return a.getEle();
    }

    public void imprimir() {
        Nodo a = tope;
        String print = "";
        while (a != null) {
            print += a.getEle() + " ";
            a = a.getNodoSig();
        }
        LogFactory.getLog(getClass()).info(print + "\n");
    }

    public int getSize() {
        return size;
    }

    public void eliminar(int index) {

        Nodo nodo = null;

        while (tope != null) {

            if (index != 0) {
                if (nodo == null) {
                    nodo = new Nodo(tope.getEle());
                } else {
                    Nodo aux = new Nodo(tope.getEle());
                    aux.setNodoSig(nodo);
                    nodo = aux;
                }
            }
            tope = tope.getNodoSig();
            index--;
        }

        while (nodo != null) {
            addicionarEle(nodo.getEle());
            nodo = nodo.getNodoSig();
        }

    }

    public void setSize(int size) {
        this.size = size;
    }
}
